import toastr from "toastr";
import axios from "axios";
import cookies from "vue-cookies";

// const BASE_URL = "http://192.168.31.55:8080";
const BASE_URL = "http://192.168.31.118:8080";
// const BASE_URL = "http://47.113.227.183:8080";

export default {
    //查询cookie登录用户 name为路由页面的名称，用于定位不同的信息出不同的提示框
    async isUserLogin() {
        if (cookies.isKey("user") == false) {
            console.log("cookies不存在!");
            return null;
        }
        return await axios
            .get(BASE_URL + "/user/isLogin",)
            .then((res) => {
                if (res.data == "0") {
                    cookies.remove("user");
                    toastr.error("身份已过期!", '请重新登录!', { timeOut: 3000 });
                    return null;
                }
                return res.data;
            })
    },

    // 登录用户
    async login(email, password) {
        return await axios
            .get(BASE_URL + "/user/loginIn", {
                params: {
                    email: email,
                    password: password,
                }
            })
            .then((res) => {
                console.log(res.data);
                if (res.data == "0") {
                    return false
                }
                cookies.set("user", res.data);
                return true
            })
            .catch((err) => {
                console.error(err);
            });
    },

    //注册用户
    async register(obj) {
        return await axios({
            method: 'post',
            url: BASE_URL + '/user/register',
            headers: { 'content-type': 'application/json' },
            data: obj
        }).then((res) => {
            if (res.data == "1") {
                return true;
            }
            toastr.error('请刷新页面重试或联系管理员', '服务器发生错误!', { timeOut: 3000 });
        })
    },

    //登出用户
    async loginOut() {
        if (cookies.isKey("user") == false) {
            window.location.reload();
        }
        return await axios
            .get(BASE_URL + "/user/loginOut", { withCredentials: true })
            .then((res) => {
                if (res.data == "1") {
                    cookies.remove("user");
                    toastr.info('再会,朋友!', '登出成功!', { timeOut: 3000 });
                    setTimeout(window.location.reload(),3000)
                    return true;
                }
                return false;
            })
            .catch((err) => {
                console.error(err);
            });

    },

    //个人主页查询各类信息 type -> 1详细信息 0是简易信息
    async getUserInfo(id, type) {
        return await axios
            .get(BASE_URL + "/user/user", {
                params: {
                    id: id,
                    type: type
                }
            })
            .then((res) => {
                return res.data
            })
    },

    //查询是否签到
    async isUserCheck() {
        return await axios
            .get(BASE_URL + "/user/isCheck")
            .then((res) => {
                // true 表示已签到
                if (res.data == "1") {
                    return true;
                }
                return false;
            })
    },

    //签到操作
    async checkIn() {
        return await axios
            .get(BASE_URL + "/user/check")
            .then((res) => {
                if (res.data == "0") {
                    toastr.error('您已经签到过了！', '错误', { timeOut: 3000 });
                    window.location.reload();
                    return false;
                }
                if (res.data == "1") {
                    toastr.success('签到成功!', '经验值+100', { timeOut: 3000 });
                    return true;
                }
                return false;
            })
    },

    //发送邮箱验证码
    async sendEmailCode(email) {
        return await axios
            .get(BASE_URL + "/user/code", {
                params: {
                    email: email
                }
            })
            .then((res) => {
                if (res.data == "success") {
                    toastr.success('验证码发送成功!', '请注意查收', { timeOut: 3000 });
                    return true
                }
                toastr.error('邮箱不存在!', '请检查邮箱格式', { timeOut: 3000 });
            })
    },

    //查询邮箱或昵称是否重复
    async affirm(field, content) {
        return await axios
            .get(BASE_URL + "/user/fieldAffirm", {
                params: {
                    field: field,
                    content: content
                }
            })
            .then((res) => {
                if (res.data == "1") {
                    return true;
                }
                return false;
            })
    },

    //查询验证码是否正确
    async code(content) {
        return await axios
            .get(BASE_URL + "/user/codeAffirm", {
                params: {
                    userCode: content,
                }
            })
            .then((res) => {
                if (res.data == "success") {
                    return true;
                }

            })
    },

    //获取我的互相关注
    async getFriends(page, id) {
        return await axios
            .get(BASE_URL + "/user/friend", {
                params: {
                    page: page,
                    id: id,
                }
            })
            .then((res) => {
                return res.data;
            })
    },

    //查询是否关注
    async isFollow(id) {
        return await axios
            .get(BASE_URL + "/state/isFollow", {
                params: {
                    id: id,
                },
            })
            .then((res) => {
                return res.data;
            })
    },

    //进行关注/取消关注
    async toFollow(id, type) {
        console.log(id, type);
        return await axios
            .get(BASE_URL + "/state/toFollow", {
                params: {
                    id: id,
                    type: type
                },
            })
            .then((res) => {
                if (res.data == "1") {
                    toastr.success('关注成功!', '', { timeOut: 3000 });
                }
                return res.data;
            })
    },

    //是否是朋友
    async isFriend(id) {
        return await axios
            .get(BASE_URL + "/state/isFriend", {
                params: {
                    id: id,
                },
            })
            .then((res) => {
                return res.data;
            })
    },

    //更新用户信息
    async updateUser(obj, type) {
        return await axios({
            method: 'post',
            url: BASE_URL + '/user/update/' + type,
            headers: { 'content-type': 'multipart/form-data' },
            data: obj,
        }).then((res) => {
            if (res.data == "1") {
                toastr.success('刷新页面更新', 'OK', { timeOut: 3000 });
                return res.data;
            }
            toastr.error('请刷新页面重试或联系管理员', '服务器发生错误!', { timeOut: 3000 });
        })
    },

    //获取用户追番列表
    async getUserBangumiList(page, id) {
        return await axios
            .get(BASE_URL + "/state/user/bangumi", {
                params: {
                    page: page,
                    id: id
                },
            })
            .then((res) => {
                return res.data;
            })
    },
}